<?php
/*
* Author: mma 
* Date: 2013-12-05 
*
* This is ach pad (debit) file model. currently, only support canada domestic transfer and CAD currency. Please check default_*** function for default value.
*
* Header and trailor are the same as pds file. Only payment record is different
*/

include_once('fundation.php');
include_once('util.php');

class ach_dom_pad{
    protected $header = '';
    protected $records = array();
    protected $trailor = '';

    public function __construct(){
        $this->header = new line;
        $this->trailor = new line;
    }
    
    public function init(){
        $this->init_header();
        $this->default_header();
        $this->init_trailor();
        $this->default_trailor();
    }

    private function init_header(){
        // init header fields. set type and length only
        $field1 = new field;
        $field1->setType(finalVar::$_NUMBER);
        $field1->setLeng(6);
        $field1->setPos(1);

        $field2 = new field;
        $field2->setType(finalVar::$_ALPHANUMERIC);
        $field2->setLeng(1);
        $field2->setPos(7);

        $field3 = new field;
        $field3->setType(finalVar::$_ALPHANUMERIC);
        $field3->setLeng(3);
        $field3->setPos(8);


        $field4 = new field;
        $field4->setType(finalVar::$_ALPHANUMERIC);
        $field4->setLeng(10);
        $field4->setPos(11);

        $field5 = new field;
        $field5->setType(finalVar::$_ALPHANUMERIC);
        $field5->setLeng(30);
        $field5->setPos(21);

        $field6 = new field;
        $field6->setType(finalVar::$_ALPHANUMERIC);
        $field6->setLeng(4);
        $field6->setPos(51);

        $field7 = new field;
        $field7->setType(finalVar::$_NUMBER);
        $field7->setLeng(7);
        $field7->setPos(55);

        $field8 = new field;
        $field8->setType(finalVar::$_ALPHANUMERIC);
        $field8->setLeng(3);
        $field8->setPos(62);

        $field9 = new field;
        $field9->setType(finalVar::$_ALPHANUMERIC);
        $field9->setLeng(1);
        $field9->setPos(65);

        $field10 = new field;
        $field10->setType(finalVar::$_ALPHANUMERIC);
        $field10->setLeng(15);
        $field10->setPos(66);

        $field11 = new field;
        $field11->setType(finalVar::$_ALPHANUMERIC);
        $field11->setLeng(6);
        $field11->setPos(81);

        $field12 = new field;
        $field12->setType(finalVar::$_ALPHANUMERIC);
        $field12->setLeng(8);
        $field12->setPos(87);

        $field13 = new field;
        $field13->setType(finalVar::$_ALPHANUMERIC);
        $field13->setLeng(9);
        $field13->setPos(95);

        $field14 = new field;
        $field14->setType(finalVar::$_ALPHANUMERIC);
        $field14->setLeng(46);
        $field14->setPos(104);

        $field15 = new field;
        $field15->setType(finalVar::$_ALPHANUMERIC);
        $field15->setLeng(2);
        $field15->setPos(150);

        $field16 = new field;
        $field16->setType(finalVar::$_ALPHANUMERIC);
        $field16->setLeng(1);
        $field16->setPos(152);

        $this->header->addField($field1);
        $this->header->addField($field2);
        $this->header->addField($field3);
        $this->header->addField($field4);
        $this->header->addField($field5);
        $this->header->addField($field6);
        $this->header->addField($field7);
        $this->header->addField($field8);
        $this->header->addField($field9);
        $this->header->addField($field10);
        $this->header->addField($field11);
        $this->header->addField($field12);
        $this->header->addField($field13);
        $this->header->addField($field14);
        $this->header->addField($field15);
        $this->header->addField($field16);
    }

    // set default value for header. needs to be called after init
    protected function default_header(){
        $fields = $this->header->getFields();
        $fields[1]->setValue('A');
        $fields[2]->setValue('HDR');
        // local account number from express
        $fields[3]->setValue('3462020000');
        $fields[4]->setValue('7873450 CANADA INC');
        $fields[5]->setValue(utilities::getFCN());
        $fields[6]->setValue(utilities::getJD());
        $fields[7]->setValue('CAD');
        $fields[8]->setValue('1');
        $fields[9]->setValue(' ');
        $fields[10]->setValue(' ');
        $fields[11]->setValue(' ');
        $fields[12]->setValue(' ');
        $fields[13]->setValue(' ');
        $fields[14]->setValue(' ');
        $fields[15]->setValue(' ');
    }

    private function init_trailor(){
        // init trailor fields. set type and length only
        $field1 = new field;
        $field1->setType(finalVar::$_NUMBER);
        $field1->setLeng(6);
        $field1->setPos(1);

        $field2 = new field;
        $field2->setType(finalVar::$_ALPHANUMERIC);
        $field2->setLeng(1);
        $field2->setPos(7);

        $field3 = new field;
        $field3->setType(finalVar::$_ALPHANUMERIC);
        $field3->setLeng(3);
        $field3->setPos(8);

        $field4 = new field;
        $field4->setType(finalVar::$_ALPHANUMERIC);
        $field4->setLeng(10);
        $field4->setPos(11);

        $field5 = new field;
        $field5->setType(finalVar::$_NUMBER);
        $field5->setLeng(6);
        $field5->setPos(21);

        $field6 = new field;
        $field6->setType(finalVar::$_NUMBER);
        $field6->setLeng(14);
        $field6->setPos(27);

        $field7 = new field;
        $field7->setType(finalVar::$_NUMBER);
        $field7->setLeng(6);
        $field7->setPos(41);

        $field8 = new field;
        $field8->setType(finalVar::$_NUMBER);
        $field8->setLeng(14);
        $field8->setPos(47);

        $field9 = new field;
        $field9->setType(finalVar::$_NUMBER);
        $field9->setLeng(2);
        $field9->setPos(61);

        $field10 = new field;
        $field10->setType(finalVar::$_NUMBER);
        $field10->setLeng(6);
        $field10->setPos(63);

        $field11 = new field;
        $field11->setType(finalVar::$_ALPHANUMERIC);
        $field11->setLeng(12);
        $field11->setPos(69);

        $field12 = new field;
        $field12->setType(finalVar::$_ALPHANUMERIC);
        $field12->setLeng(6);
        $field12->setPos(81);

        $field13 = new field;
        $field13->setType(finalVar::$_ALPHANUMERIC);
        $field13->setLeng(63);
        $field13->setPos(87);

        $field14 = new field;
        $field14->setType(finalVar::$_ALPHANUMERIC);
        $field14->setLeng(2);
        $field14->setPos(150);

        $field15 = new field;
        $field15->setType(finalVar::$_ALPHANUMERIC);
        $field15->setLeng(1);
        $field15->setPos(152);
        
        $this->trailor->addField($field1);
        $this->trailor->addField($field2);
        $this->trailor->addField($field3);
        $this->trailor->addField($field4);
        $this->trailor->addField($field5);
        $this->trailor->addField($field6);
        $this->trailor->addField($field7);
        $this->trailor->addField($field8);
        $this->trailor->addField($field9);
        $this->trailor->addField($field10);
        $this->trailor->addField($field11);
        $this->trailor->addField($field12);
        $this->trailor->addField($field13);
        $this->trailor->addField($field14);
        $this->trailor->addField($field15);
    }

    // set default value for trailor. needs to be called after init
    protected function default_trailor(){
        $fields = $this->trailor->getFields();
        $fields[1]->setValue('Z');
        $fields[2]->setValue('TRL');
        $fields[3]->setValue('3462020000');
        $fields[4]->setValue('0');
        $fields[5]->setValue('0');
        $fields[8]->setValue('0');
        $fields[9]->setValue('0');
        $fields[10]->setValue(' ');
        $fields[11]->setValue(' ');
        $fields[12]->setValue(' ');
        $fields[13]->setValue(' ');
        $fields[14]->setValue(' ');
    }

    public function init_record(){
        $rec = new line;

        // init payment record fields. set type and length only
        $field1 = new field;
        $field1->setType(finalVar::$_NUMBER);
        $field1->setLeng(6);
        $field1->setPos(1);

        $field2 = new field;
        $field2->setType(finalVar::$_ALPHANUMERIC);
        $field2->setLeng(1);
        $field2->setPos(7);

        $field3 = new field;
        $field3->setType(finalVar::$_ALPHANUMERIC);
        $field3->setLeng(3);
        $field3->setPos(8);

        $field4 = new field;
        $field4->setType(finalVar::$_ALPHANUMERIC);
        $field4->setLeng(10);
        $field4->setPos(11);

        $field5 = new field;
        $field5->setType(finalVar::$_ALPHANUMERIC);
        $field5->setLeng(1);
        $field5->setPos(21);

        $field6 = new field;
        $field6->setType(finalVar::$_ALPHANUMERIC);
        $field6->setLeng(19);
        $field6->setPos(22);

        $field7 = new field;
        $field7->setType(finalVar::$_NUMBER);
        $field7->setLeng(2);
        $field7->setPos(41);

        $field8 = new field;
        $field8->setType(finalVar::$_NUMBER);
        $field8->setLeng(4);
        $field8->setPos(43);

        $field9 = new field;
        $field9->setType(finalVar::$_NUMBER);
        $field9->setLeng(5);
        $field9->setPos(47);

        $field10 = new field;
        $field10->setType(finalVar::$_ALPHANUMERIC);
        $field10->setLeng(18);
        $field10->setPos(52);

        $field11 = new field;
        $field11->setType(finalVar::$_ALPHANUMERIC);
        $field11->setLeng(1);
        $field11->setPos(70);

        $field12 = new field;
        $field12->setType(finalVar::$_NUMBER);
        $field12->setLeng(10);
        $field12->setPos(71);

        $field13 = new field;
        $field13->setType(finalVar::$_ALPHANUMERIC);
        $field13->setLeng(6);
        $field13->setPos(81);

        $field14 = new field;
        $field14->setType(finalVar::$_NUMBER);
        $field14->setLeng(7);
        $field14->setPos(87);

        $field15 = new field;
        $field15->setType(finalVar::$_ALPHANUMERIC);
        $field15->setLeng(30);
        $field15->setPos(94);

        $field16 = new field;
        $field16->setType(finalVar::$_ALPHANUMERIC);
        $field16->setLeng(1);
        $field16->setPos(124);

        $field17 = new field;
        $field17->setType(finalVar::$_ALPHANUMERIC);
        $field17->setLeng(1);
        $field17->setPos(125);

        $field18 = new field;
        $field18->setType(finalVar::$_ALPHANUMERIC);
        $field18->setLeng(15);
        $field18->setPos(126);

        $field19 = new field;
        $field19->setType(finalVar::$_ALPHANUMERIC);
        $field19->setLeng(3);
        $field19->setPos(141);

        $field20 = new field;
        $field20->setType(finalVar::$_ALPHANUMERIC);
        $field20->setLeng(1);
        $field20->setPos(144);

        $field21 = new field;
        $field21->setType(finalVar::$_ALPHANUMERIC);
        $field21->setLeng(3);
        $field21->setPos(145);

        $field22 = new field;
        $field22->setType(finalVar::$_ALPHANUMERIC);
        $field22->setLeng(2);
        $field22->setPos(148);

        $field23 = new field;
        $field23->setType(finalVar::$_ALPHANUMERIC);
        $field23->setLeng(2);
        $field23->setPos(150);

        $field24 = new field;
        $field24->setType(finalVar::$_ALPHANUMERIC);
        $field24->setLeng(1);
        $field24->setPos(152);

        $rec->addField($field1);
        $rec->addField($field2);
        $rec->addField($field3);
        $rec->addField($field4);
        $rec->addField($field5);
        $rec->addField($field6);
        $rec->addField($field7);
        $rec->addField($field8);
        $rec->addField($field9);
        $rec->addField($field10);
        $rec->addField($field11);
        $rec->addField($field12);
        $rec->addField($field13);
        $rec->addField($field14);
        $rec->addField($field15);
        $rec->addField($field16);
        $rec->addField($field17);
        $rec->addField($field18);
        $rec->addField($field19);
        $rec->addField($field20);
        $rec->addField($field21);
        $rec->addField($field22);
        $rec->addField($field23);
        $rec->addField($field24);
        $this->default_record($rec);

        return $rec;
    }

    protected function default_record($rec){
        $fields = $rec->getFields();
        $fields[1]->setValue('D');
        $fields[2]->setValue('450');
        $fields[3]->setValue('3462020000');
        $fields[4]->setValue(' ');
        $fields[6]->setValue(utilities::getPN());
        $fields[10]->setValue(' ');
        $fields[12]->setValue(' ');
        $fields[15]->setValue('E');
        $fields[16]->setValue(' ');
        $fields[17]->setValue('Cash2u payment');
        $fields[18]->setValue('CAD');
        $fields[19]->setValue(' ');
        $fields[20]->setValue('CAN');
        $fields[21]->setValue(' ');
        $fields[22]->setValue(' ');
        $fields[23]->setValue('N');
    }

    public function addRecord($rec){
        array_push($this->records, $rec);
    }

    public function getRecords(){
        return $this->records;
    }

    public function setRecords($recs){
        $this->records = $recs;
    }

    public function getHeader(){
        return $this->header;
    }

    public function setHeader($line){
        $this->header = $line;
    }

    public function getTrailor(){
        return $this->trailor;
    }

    public function setTrailor($line){
        $this->trailor = $line;
    }

    public function __toString(){
        $ret = '';
        $ret .= $this->getHeader();
        foreach ($this->getRecords() as $record){
            $ret .= $record;
        }
        $ret .= $this->getTrailor();

        return $ret;
    }
}
?>